package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.CommentDao;
import com.guochenglang.www.dao.ReplyDao;
import com.guochenglang.www.dao.ReplyMessageDao;
import com.guochenglang.www.pojo.Comment;
import com.guochenglang.www.pojo.Reply;
import com.guochenglang.www.pojo.ReplyMessage;
import com.guochenglang.www.service.ReplyService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.DangerWordBoolean;
import com.guochenglang.www.util.MyDependency;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 回复业务
 *
 * @author GuoChenglang
 */
public class ReplyServiceImpl implements ReplyService {
    private static final ReplyDao REPLY_DAO = MyDependency.returnObject(ReplyDao.class);
    private static final ReplyMessageDao REPLY_MESSAGE_DAO = MyDependency.returnObject(ReplyMessageDao.class);

    /**
     * 获得回复列表
     *
     * @param conditions 搜索信息
     * @return 获得回复列表
     */
    @Override
    public ArrayList<Reply> getReply(Reply conditions) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return REPLY_DAO.replySelect(conditions);
    }

    /**
     * 进行回复
     *
     * @param reply 回复的信息
     * @return 主键
     */
    @Affair
    @Override
    public int uploadReply(Reply reply) throws SQLException, IllegalAccessException, InstantiationException, InterruptedException {
        //敏感词判断
        String determine = reply.getReply();
        if (DangerWordBoolean.isDangerWord(determine)) {
            return -1;
        }
        //获得回复的主键
        int key = REPLY_DAO.insertReply(reply);
        //获得对应评论
        Integer userId = reply.getReplyUser();
        //对被回复用户进行提醒
        REPLY_MESSAGE_DAO.insertReplyMessage(new ReplyMessage(null, 0, key, userId));
        return key;
    }

    /**
     * 删除回复
     *
     * @param replyId 回复的id
     * @return 删除行数
     */
    @Affair
    @Override
    public int deleteReply(Integer replyId) throws SQLException, IllegalAccessException, InstantiationException, InterruptedException {
        ReplyMessage messageCondition = new ReplyMessage();
        messageCondition.setReplyId(replyId);
        Reply replyCondition = new Reply();
        replyCondition.setId(replyId);
        //删除消息提醒
        REPLY_MESSAGE_DAO.deleteReplyMessage(messageCondition);
        //删除回复
        return REPLY_DAO.deleteReply(replyCondition);
    }
}
